package com.hktf.uilib.callback;

import com.hktf.corelib.jsonbean.BaseResultNew;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.GsonUtils;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.NetWorkUtils;
import com.hktf.uilib.R;
import com.hktf.uilib.utils.DlgUtils;
import com.hktf.uilib.utils.ToastUtils;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.lang.reflect.ParameterizedType;

/**
 * date: 2022/9/19 15:48.
 *
 * @auther: cdz
 * @function:
 */
public class BaseCallBackNew<T> implements Callback<T> {
    private Class<T>  type;

    public BaseCallBackNew() {
    }

    public BaseCallBackNew(Class<T> type) {
        this.type = type;
    }

    @Override
    public void onStart(Request<T, ? extends Request> request) {

    }

    @Override
    public void onSuccess(Response<T> response) {
        T body = response.body();
        if (body == null) {
            onError(new Exception("response.body=null"));
            return;
        }
        try {
            BaseResultNew result = (BaseResultNew) body;
            if (result.isSuccessCode()) {
                onSUCCESS(response);
            } else {
                // TODO: 2022/9/19 错误情况
//                EventBusUtil.showOnResponseFail(result.getRESULT());
//                Class<?> aClass = Class.forName("com.hktf.uilib.engine.CommServiceExceptionHandler");
//                Method handerServiceException = aClass.getDeclaredMethod("handerServiceException", Integer.class);
//                handerServiceException.setAccessible(true);
//                Object invoke = handerServiceException.invoke(null, result.getCode());
//                if (invoke == null) {
//                    onResponseFail("服务器返回错误码：" + result.getCode(),result.getMsg()+"");
//                } else {
//                    onResponseFail("服务器返回错误码：" + result.getCode(),(String) invoke);
//                }
                onResponseFail("服务器返回错误码：" + result.getCode(),result.getMsg()+"");
            }
            return;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        onError(new Exception("服务器返回数据异常"));
    }




    @Override
    public void onCacheSuccess(Response<T> response) {

    }

    @Override
    public void onError(Response<T> response) {
        if (!NetWorkUtils.isConnectingToInternet(CommUtils.getContext())) {
            onResponseFail(CommUtils.getString(R.string.network_no_connected),CommUtils.getString(R.string.network_no_connected));
            return;
        }
        Throwable exception = response.getException();
        LogMds.i("cdz", "error:" + exception.getMessage());
        onError(response.getException());
    }

    @Override
    public void onFinish() {

    }

    @Override
    public void uploadProgress(Progress progress) {

    }

    @Override
    public void downloadProgress(Progress progress) {

    }

    @Override
    public T convertResponse(okhttp3.Response response) throws Throwable {
        ParameterizedType genericSuperclass = null;
        try {
//            LogMds.d("cdz", "class:" + getClass().getGenericSuperclass());
//            genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
//            return GsonUtils.jsonToBean(response.body().string(), (Class<T>) genericSuperclass.getActualTypeArguments()[0]);
            return GsonUtils.jsonToBean(response.body().string(), type);
        } catch (Exception exception) {
            exception.printStackTrace();
            onError(exception);
        }
        return null;
    }


    public void onSUCCESS(Response<T> response){
        DlgUtils.disMissDlg();
        ToastUtils.showToast(CommUtils.getContext(),CommUtils.getString(R.string.op_success),2000);
//        Toast.makeText(CommUtils.getContext(), CommUtils.getString(R.string.op_success), Toast.LENGTH_SHORT);
    }

    public void onError(Throwable exception) {
        ToastUtils.showToast(CommUtils.getContext(),CommUtils.getString(R.string.network_error),2000);
        DlgUtils.disMissDlg();
    }

    public void onResponseFail(String msg, String errorcode) {
        DlgUtils.disMissDlg();
        ToastUtils.showToast(CommUtils.getContext(),errorcode+"",2000);
    }
}
